package com.sgb.member.view_model

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.sgb.member.MemberConfig
import com.sgb.member.entity.ListMResponse
import com.sgb.member.entity.RemoveMemberBean
import com.swgk.core.base.api.APIManager
import com.swgk.core.base.api.ICallBack
import com.swgk.core.base.model.entity.BaseEntity
import com.swgk.core.base.model.entity.H5ReEntity
import com.swgk.core.util.MToast
import com.swgk.core.view.selection.CommonPopEntity
import java.util.*
import kotlin.collections.ArrayList

class RemoveMemberModel : ViewModel() {
    var msgLiveData = MutableLiveData<String>()
    var isLoadLiveData = MutableLiveData<Boolean>()
    var tabDatasLiveData = MutableLiveData<List<CommonPopEntity?>?>()
    var datasLiveData = MutableLiveData<List<RemoveMemberBean?>?>()
    val urlH5LiveData = MutableLiveData<String>()
    private var current = 0


    // 会员类型
    fun getTabData(type: String) {//供需关系1:供方2:需方
        var pos ="1"   // 0 供方 1需方
        if(type=="1"){
            pos ="0"
        }
        MemberConfig.getInstance().api.getMemberTypeList2(pos, "")
            .enqueue(object : ICallBack<BaseEntity<List<CommonPopEntity?>?>>() {
                override fun onSuccess(data: BaseEntity<List<CommonPopEntity?>?>) {
                    tabDatasLiveData.postValue(data.data)
                }
            })
    }

    fun loadDatas(
        isLoadMore: Boolean, type: String, oneTab: String,
        twoTab: String, name: String
    ) {
        isLoadLiveData.postValue(true)
        val currentPage = if (isLoadMore) current + 1 else 1
        val map = mutableMapOf<String, Any>()
        map["name"] = name
        map["firstCategoty"] = oneTab
        map["secondCategoty"] = twoTab
        map["relation"]=type//供需关系1:供方2:需方
        map["limit"] = 10
        map["page"] = currentPage
        MemberConfig.getInstance().api.getDelRelationByPage(map)
            .enqueue(object : ICallBack<BaseEntity<ListMResponse<List<RemoveMemberBean>>>>() {
                override fun onSuccess(data: BaseEntity<ListMResponse<List<RemoveMemberBean>>>?) {
                    isLoadLiveData.postValue(false)
                    current = data?.data?.current?:1
                    data?.let {
                        current = it.data.current
                        datasLiveData.postValue(it.data.list)
                    }
                }

                override fun onFail() {
                    super.onFail()
                    isLoadLiveData.postValue(false)
                    msgLiveData.postValue("请求失败")
                }
            })
    }

    fun getUrlSetting(param: String?, endUrl: String) {
        MemberConfig.getInstance().api.getUrlSetting(param)
            .enqueue(object : ICallBack<BaseEntity<H5ReEntity>?>() {
                override fun onSuccess(data: BaseEntity<H5ReEntity>?) {
                    if (data == null || data.state == null) {
                        return
                    }
                    if (data.state == APIManager.RESPONSE_OK) {
                        val url = data.data.url + endUrl
                        urlH5LiveData.postValue(url)
                       // urlH5LiveData.postValue("http://mobile.gcw.net/removedDetail?no=1483335511685861376")
                    } else {
                        msgLiveData.postValue(data.msg)
                    }
                }
            })
    }


}